/**
 * Applikationsutveckling i Java, 5DV135
 * HT12
 * Project
 *
 * File: Junction.java
 * Description: A class for representing a changeable road junction
 * 				in a game
 * Date: 18 december 2012
 *
 * cs-user: dv11mgr
 * @author Maria Gramner
 */
package nullInvader.Engine;
import java.awt.Image;
import java.awt.Point;
import java.io.IOException;
import javax.imageio.ImageIO;


public class Junction extends Bricks {

    private String currentDirection;
    private final Point nextPosition;
    private Point position;
    private Image icon;

    /**
     * Constructor, sets the movability of the junction, and the
     * direction of which the junction is currently directing
     * "traffic".
     * @param movable boolean
     * @param direction String. For example "south" if it
     * should point south.
     */
    public Junction(boolean movable, String direction){
        super(movable);
        currentDirection = direction;
        position = new Point();
        nextPosition = new Point();
        setIcon();
    }


    /**
     * Method that sets the correct image for the class depending on
     * which direction it is currently pointing towards
     */
    protected void setIcon(){
        if (currentDirection != null){
            /*
             * If current direction is north, set the picture to up
             */
            if (currentDirection.equals("north")){
                try{
                    icon = ImageIO.read(getClass().getResource(
                            "/nullInvader/Resources/arrowUp.jpg"));
                } catch (IOException e){
                    System.out.println("Picture arrowUp.jpg doesn't exist");
                }
            }
            /*
             * If current direction is east, set the picture to right
             */
            else if (currentDirection.equals("east")){
                try{
                    icon = ImageIO.read(getClass().getResource(
                            "/nullInvader/Resources/arrowRight.jpg"));
                } catch (IOException e){
                    System.out.println("Picture arrowRight.jpg doesn't exist");
                }
            }
            /*
             * If current direction is south, set the picture to down
             */
            else if (currentDirection.equals("south")){
                try{
                    icon = ImageIO.read(getClass().getResource(
                            "/nullInvader/Resources/arrowDown.jpg"));
                } catch (IOException e){
                    System.out.println("Picture arrowDown.jpg doesn't exist");
                }
            }
            /*
             * If current direction is west, set the picture to left
             */
            else if (currentDirection.equals("west")){
                try{
                    icon = ImageIO.read(getClass().getResource(
                            "/nullInvader/Resources/arrowLeft.jpg"));
                } catch (IOException e){
                    System.out.println("Picture arrowLeft.jpg doesn't exist");
                }
            }
        }
    }


    /**
     * Method to get the image of the junction
     * @return Image
     */
    public Image getIcon(){
        return icon;
    }


    /**
     * Method that directs the unit to walk in the specified
     * direction.
     * @param unit Unit. The unit which is being directed
     */
    public void landOn(Unit unit){
        position = unit.getPosition();

        /* Set nextPosition to the corresponding direction */
        if (currentDirection.equals("north")){
            nextPosition.setLocation(position.getX(), (position.getY() - 1));
        } else if (currentDirection.equals("south")){
            nextPosition.setLocation(position.getX(), (position.getY() + 1));
        } else if (currentDirection.equals("east")){
            nextPosition.setLocation((position.getX() + 1), position.getY());
        } else if (currentDirection.equals("west")){
            nextPosition.setLocation((position.getX() - 1), position.getY());
        }

        unit.setNextPosition(nextPosition);
    }

    /**
     * Method to get the current direction which the junction is
     * pointing to
     * @return String
     */
    public String getCurrentDirection() {
        return currentDirection;
    }

    /**
     * Method to change the direction the junction is pointing to when
     * the object is being clicked on, and change to the corresponding
     * image
     */
    public void wasClicked() {
        /* If the direction is north, change to south */
        if (currentDirection.equals("north")) {
            currentDirection = "south";
            setIcon();
        }
        /* If the direction is south, change to north */
        else if (currentDirection.equals("south")) {
            currentDirection = "north";
            setIcon();
        }
        /* If the direction is east, change to west */
        else if (currentDirection.equals("east")) {
            currentDirection = "west";
            setIcon();
        }
        /* If the direction is west, change to east */
        else if (currentDirection.equals("west")) {
            currentDirection = "east";
            setIcon();
        }
    }
}
